package com.weiyi.mashangzhu.Roomer.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Bean.LandlordInfoBean;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.weiyi.mashangzhu.Weight.scrollview.MonitorScrollView;
import com.weiyi.mashangzhu.Weight.scrollview.listener.OnScrollListener;
import com.zhy.android.percent.support.PercentLinearLayout;
import com.zhy.android.percent.support.PercentRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.igexin.sdk.GTServiceManager.context;

public class LandlordInfoActivity extends BaseActivity {


    @BindView(R.id.title_bar)
    PercentLinearLayout titleBar;
    @BindView(R.id.scrollview)
    MonitorScrollView scrollview;
    @BindView(R.id.layout_img)
    PercentRelativeLayout layoutImg;
    @BindView(R.id.tv_top)
    TextView tvTop;
    @BindView(R.id.more_house)
    PercentLinearLayout moreHouse;
    @BindView(R.id.scrollView)
    HorizontalScrollView scrollView;
    @BindView(R.id.head)
    ImageView head;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.goodcomment)
    TextView goodcomment;
    @BindView(R.id.reurn1)
    TextView reurn1;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.age)
    TextView age;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.name_comment)
    TextView nameComment;
    @BindView(R.id.time_comment)
    TextView timeComment;
    @BindView(R.id.content_comment)
    TextView contentComment;
    LandlordInfoBean landlordInfoBean;
    String id, city;
    List<String> urllist = new ArrayList<>();
    List<String> ids = new ArrayList<>();
//    boolean[] flags;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landlord_info);
        ButterKnife.bind(this);
        tvTop.setVisibility(View.GONE);
        id = getIntent().getStringExtra("id");
        city = getIntent().getStringExtra("city");
        initView();
        scrollChangeBarBg();
    }

    @Override
    protected void onResume() {
        super.onResume();
        moreHouse.removeAllViews();
        initView_resume();
    }

    public void initView_resume() {
        LoadingDialog.show(this);
        OkHttpUtils.get().url(API.BaseApi_R + API.LandlordInfo_Api).addParams("token", SPUtils.get(this, "token", "") + "").addParams("sellerId", id).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                LoadingDialog.dismiss(LandlordInfoActivity.this);
                T.showShort(LandlordInfoActivity.this, "网络连接失败，请检查一下网络设置");
            }

            @Override
            public void onResponse(String response) {
                Log.e("pppppppppppppppppp", "===" + response);
                LoadingDialog.dismiss(LandlordInfoActivity.this);
                Gson gson = new Gson();
                landlordInfoBean = gson.fromJson(response, LandlordInfoBean.class);
                List<LandlordInfoBean.DataBean.OtherhouselistBean> otherhouselist = new ArrayList<LandlordInfoBean.DataBean.OtherhouselistBean>();
                otherhouselist.clear();
                otherhouselist = landlordInfoBean.getData().getOtherhouselist();
//                LandlordInfoBean.DataBean.HostBean hostbean = landlordInfoBean.getData().getHost();
//                List<LandlordInfoBean.DataBean.EvalistBean> e = new ArrayList<LandlordInfoBean.DataBean.EvalistBean>();
//                e = landlordInfoBean.getData().getEvalist();
                initOtherHouse(otherhouselist);
//                GlideImgManager.glideLoader(LandlordInfoActivity.this, API.BaseApi_url + hostbean.getUHead(), R.mipmap.errer, head, 0);
//                name.setText(hostbean.getUNickname() + "");
//                goodcomment.setText(landlordInfoBean.getData().getHostrat() + "");
//                reurn1.setText(landlordInfoBean.getData().getReply() + "");
//                if ((hostbean.getUSex() + "").equals("1")) {
//                    sex.setText("男");
//                } else if ((hostbean.getUSex() + "").equals("0")) {
//                    sex.setText("女");
//                }
//                age.setText(hostbean.getUAge() + "");
//                address.setText(hostbean.getUCity() + "");
//                if (e.size() != 0) {
//                    nameComment.setText(e.get(0).getCustomerName() + "");
//                    timeComment.setText(e.get(0).getETime() + "");
//                    contentComment.setText(e.get(0).getEContent() + "");
//                }
//
//
            }
        });
    }

    @Override
    public void initView() {
        super.initView();
        LoadingDialog.show(this);
        OkHttpUtils.get().url(API.BaseApi_R + API.LandlordInfo_Api).addParams("token", SPUtils.get(this, "token", "") + "").addParams("sellerId", id).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                LoadingDialog.dismiss(LandlordInfoActivity.this);
                T.showShort(LandlordInfoActivity.this, "网络连接失败，请检查一下网络设置");
            }

            @Override
            public void onResponse(String response) {
                Log.e("pppppppppppppppppp", "===" + response);
                LoadingDialog.dismiss(LandlordInfoActivity.this);
                Gson gson = new Gson();
                landlordInfoBean = gson.fromJson(response, LandlordInfoBean.class);
//                List<LandlordInfoBean.DataBean.OtherhouselistBean> otherhouselist = new ArrayList<LandlordInfoBean.DataBean.OtherhouselistBean>();
//                otherhouselist.clear();
//                otherhouselist = landlordInfoBean.getData().getOtherhouselist();
//                initOtherHouse(otherhouselist);
                LandlordInfoBean.DataBean.HostBean hostbean = landlordInfoBean.getData().getHost();
                List<LandlordInfoBean.DataBean.EvalistBean> e = new ArrayList<LandlordInfoBean.DataBean.EvalistBean>();
                e = landlordInfoBean.getData().getEvalist();

                GlideImgManager.glideLoader(LandlordInfoActivity.this, API.BaseApi_url + hostbean.getUHead(), R.mipmap.errer, head, 0);
                name.setText(hostbean.getUNickname() != null ? hostbean.getUNickname() : "");
                goodcomment.setText(landlordInfoBean.getData().getHostrat() != null ? landlordInfoBean.getData().getHostrat() : "0");
                reurn1.setText(landlordInfoBean.getData().getReply() != null ? landlordInfoBean.getData().getReply() : "0");
                if ((hostbean.getUSex() + "").equals("1")) {
                    sex.setText("男");
                } else if ((hostbean.getUSex() + "").equals("0")) {
                    sex.setText("女");
                }
                age.setText(hostbean.getUAge() + "");
                address.setText(hostbean.getUCity() != null ? hostbean.getUCity() : "");
                if (e.size() != 0) {
                    nameComment.setText(e.get(0).getCustomerName() != null ? e.get(0).getCustomerName() : "");
                    timeComment.setText(e.get(0).getETime() != null ? e.get(0).getETime() : "");
                    contentComment.setText(e.get(0).getEContent() != null ? e.get(0).getEContent() : "");
                }


            }
        });
    }

    //初始化其他房源
    private void initOtherHouse(final List<LandlordInfoBean.DataBean.OtherhouselistBean> otherhouselist) {

        Log.e("ppppppppppppppppppp", "size" + otherhouselist.size());
        for (int i = 0; i < otherhouselist.size(); i++) {
            ids.add(otherhouselist.get(i).getId() + "");
        }
        LayoutInflater inflater = LayoutInflater.from(LandlordInfoActivity.this);
//        flags = new boolean[otherhouselist.size()];

        for (int i = 0; i < otherhouselist.size(); i++) {
            urllist.clear();
//            flags[i] = false;
            View view = inflater.inflate(R.layout.item_housing_list, null);
            TextView num = (TextView) view.findViewById(R.id.price_num);
            final ImageButton collect = (ImageButton) view.findViewById(R.id.housing_mark);
            ImageView img = (ImageView) view.findViewById(R.id.housing_img);
            TextView title = (TextView) view.findViewById(R.id.housing_title);
            TextView grade = (TextView) view.findViewById(R.id.housing_grade);
            TextView type = (TextView) view.findViewById(R.id.housing_type);
            if (otherhouselist != null && otherhouselist.size() != 0) {
                num.setText("¥" + otherhouselist.get(i).getHPrice() + "");
                if (otherhouselist.get(i).getIsCollect().equals("0")) {
                    collect.setImageResource(R.mipmap.collect);
                } else if (otherhouselist.get(i).getIsCollect().equals("1")) {
                    collect.setImageResource(R.mipmap.collect1);
                }
                if (otherhouselist.get(i).getHImg1() != null) {
                    if (!otherhouselist.get(i).getHImg1().equals("")) {
                        urllist.add(otherhouselist.get(i).getHImg1());
                    }
                }
                if (otherhouselist.get(i).getHImg2() != null) {
                    if (!otherhouselist.get(i).getHImg2().equals("")) {
                        urllist.add(otherhouselist.get(i).getHImg2());
                    }

                }
                if (otherhouselist.get(i).getHImg3() != null) {
                    if (!otherhouselist.get(i).getHImg3().equals("")) {
                        urllist.add(otherhouselist.get(i).getHImg3());
                    }

                }
                if (otherhouselist.get(i).getHImg4() != null) {
                    if (!otherhouselist.get(i).getHImg4().equals("")) {
                        urllist.add(otherhouselist.get(i).getHImg4());
                    }

                }
                if (otherhouselist.get(i).getHImg5() != null) {
                    if (!otherhouselist.get(i).getHImg5().equals("")) {
                        urllist.add(otherhouselist.get(i).getHImg5());
                    }

                }
                if (otherhouselist.get(i).getHImg6() != null) {
                    if (!otherhouselist.get(i).getHImg5().equals("")) {
                        urllist.add(otherhouselist.get(i).getHImg6());
                    }

                }
                GlideImgManager.glideLoader(LandlordInfoActivity.this, API.BaseApi_url + urllist.get(0), R.mipmap.errer, img);
                title.setText(otherhouselist.get(i).getHTitle() + "");
                grade.setText(otherhouselist.get(i).getScoreave() + "");
                moreHouse.addView(view);
                type.setText(otherhouselist.get(i).getHRoom() + "居  |  " + otherhouselist.get(i).getHDistrict());
                final int finalI = i;
                final int finalI1 = i;
                collect.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (SPUtils.get(LandlordInfoActivity.this, "isLogin", "0").equals("0")) {
                            UiUtil.startActivity(LandlordInfoActivity.this, LoginActivity.class);
                        } else if (SPUtils.get(LandlordInfoActivity.this, "isLogin", "0").equals("1")) {

                            //已登录 进行收藏操作
                            OkHttpUtils.get()
                                    .addParams("token", SPUtils.get(LandlordInfoActivity.this, "token", "") + "")
                                    .addParams("id", otherhouselist.get(finalI1).getId() + "")
                                    .url(API.BaseApi_R + API.CollectHouse_Api)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            T.showShort(context, "网络连接失败,请稍候再试");

                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            try {
                                                JSONObject jsonObject = new JSONObject(response);
                                                if (jsonObject.getInt("status") == 0) {
                                                    if (jsonObject.getString("msg").equals("收藏成功")) {
                                                        collect.setImageResource(R.mipmap.collect1);
                                                        otherhouselist.get(finalI1).setIsCollect("1");
                                                        T.showShort(LandlordInfoActivity.this, "收藏成功");
                                                    } else {
                                                        collect.setImageResource(R.mipmap.collect);
                                                        otherhouselist.get(finalI1).setIsCollect("0");
                                                        T.showShort(LandlordInfoActivity.this, "取消收藏成功");
                                                    }
                                                } else {
                                                    T.showShort(context, jsonObject.getString("msg"));
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            Log.e("rrrrrrrrrrr", "==" + response);
                                        }
                                    });
                        }
                    }
                });
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent i = new Intent(LandlordInfoActivity.this, HouseDetailsActivity.class);
                        i.putExtra("id", ids.get(finalI) + "");
                        i.putExtra("city", city);
                        LandlordInfoActivity.this.startActivity(i);

                    }
                });
            }

        }
    }

    private void scrollChangeBarBg() {
        scrollview.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScroll() {
                int[] ints = new int[2];
                layoutImg.getLocationOnScreen(ints);
                int scrollY = -ints[1];
                int imageHeight = layoutImg.getHeight();
                if (layoutImg != null && imageHeight > 0) {
                    if (scrollY <= 0) {
                        titleBar.getBackground().setAlpha(0);
                        tvTop.setVisibility(View.GONE);
                    } else {
                        if (scrollY < imageHeight) {
                            float progress = new Float(scrollY) / new Float(imageHeight) * 255;
                            titleBar.getBackground().setAlpha((int) progress);
                            tvTop.setVisibility(View.VISIBLE);
                        } else {
                            titleBar.getBackground().setAlpha(255);
                            tvTop.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.all_comment})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.all_comment:
                intent = new Intent(this, AllCommentActivity.class);
                intent.putExtra("type", "host");
                intent.putExtra("id", id);
                startActivity(intent);
                break;
        }
    }
}
